<script lang="ts">
import { defineComponent, ref, watch, onMounted, onUnmounted } from 'vue'
import { getHot } from '../api/home'
import { useRouter } from 'vue-router'
import { viewCount, filters, vidotime } from '../hooks/filters'
import { getimage } from '../hooks/image'
import { storeToRefs } from 'pinia';
import { useCounterStore } from '../stores/home'
import { getVideo } from '../api/videodes'
import { useScrollStore } from '../stores/scroll'
export default defineComponent({
  components: {},
  setup() {

    const store = useCounterStore()
    const { idarr, likearr, collectlikearr } = storeToRefs(store)
    // 作为 action 的 increment 可以直接解构
    const { increment, incrementfalse, unlikeFuns, likeFuns, collectFuns, uncollectFuns } = store;
    // 关注部分开始


    // 点击关注
    let attentiontaggtrue = (id: any) => {
      increment(id);
    }
    // 取消关注
    let attentiontaggfalse = (id: any) => {
      incrementfalse(id)
    }
    // 关注部分结束

    // // 点击喜欢
    let likeFun = (id: any) => {
      likeFuns(id)

    }
    // 取消喜欢
    let unlikeFun = (id: any) => {
      unlikeFuns(id)
    }
    const storeScroll = useScrollStore();
    const { scrollIndex, videoList } = storeToRefs(storeScroll);
    const { changeIndex } = storeScroll;
    let scrollTop = ref<number>(0);
    let currentIndex = ref<number>(0);
    let onscroll = () => {
      scrollTop.value = document.documentElement.scrollTop;
      currentIndex.value = Math.round((scrollTop.value - 300) / 332);
      changeIndex(currentIndex.value, 300);
    }

    //挂载开启滚动事件
    onMounted(() => {
      // console.log('isReachBottom', isReachBottom.value)
      window.addEventListener('scroll', onscroll);
    })
    //销毁移出滚动事件
    onUnmounted(() => {
      window.removeEventListener('scroll', onscroll)
    })


    let banners: any = ref(null);
    let hotlist: any = ref(null);
    let router = useRouter();
    //获取热门数据
    let getHotFun = () => {
      getHot().then((data: any) => {
        console.log('获取热门内容data=>', data)
        banners.value = data.data.children.filter((item: any) => {
          return item.type == 'uiBanner'
        })[0]
        console.log('轮播图内容banners=>', banners.value)
        hotlist.value = data.data.children.filter((item: any) => {
          return item.type == 'uiBigCard'
        });
        hotlist.value.forEach((item: any) => {
          getVideoUrl(item.children[0].model.resource.id)
        });
        console.log('热门列表内容hotlist=>', hotlist.value)
      });
    };
    let videolisturl: any = ref([]);
    let getVideoUrl: any = (videoid: number) => {
      getVideo(videoid).then((data) => {
        videolisturl.value.push(data.data.video.content.progressive[0].url)
      });

    }
    //点击热门的视频
    let click_Hot_video = (videoid: number, video_library_id: any) => {
      console.log(videoid)
      router.push({
        path: '/videoDes',
        query: {
          aid: videoid,
          mid: video_library_id
        }
      })
    };
    let catehot = () => {
      router.push({
        path: '/catehot'
      })
    }
    let cateedit = () => {
      router.push({
        path: '/cateedit'
      })
   
    }
    let catepicker = () => {
      router.push({
        path: '/HotPicker'
      })
    }
    let click_user = (userid: any) => {
      router.push({
        path: '/userdetail',
        query: {
          userid: userid,
          active: 1
        }
      })
    }
    let istoken=localStorage.getItem('token')!=null;
    console.log('istoken=>',istoken)
    onMounted(() => {
      getHotFun() //获取热门内容
    })
    return {
      banners,
      hotlist,
      click_Hot_video,
      catehot,
      cateedit,
      getimage,
      viewCount,
      vidotime,
      click_user,
      idarr,
      likearr,
      collectlikearr,
      likeFun,
      unlikeFun,
      attentiontaggtrue,
      attentiontaggfalse,
      collectFuns,
      uncollectFuns,

      scrollIndex,
      getVideoUrl,
      videoList,
      videolisturl,
      onscroll,
      catepicker,
      istoken
    }
  }
})
</script>

<template>
  <van-loading size="24px" v-if="hotlist==undefined" style="text-align: center;line-height:40px;">加载中...</van-loading>

  <div class="hotcom" @scroll="onscroll" v-else>
    <!-- 轮播图部分 -->
    <div class="banners" v-if="banners">
      <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white" :show-indicators="false">
        <van-swipe-item v-for="(item, index) in banners.children" :key="index">
          <!-- <img src="../assets/images/swiper1.webp" /> -->
          <img :src="item.model.cover " />
        </van-swipe-item>
      </van-swipe>
    </div>
    <!-- 中间按钮 -->
    <div class="btnlist">
      <ul>
        <li>
          <img src="https://oss-xpc6.xpccdn.com/default/Q14gOkU59kQFoEjFdaIQfvIo5Gvy7cA1fOQdSPD9.png" alt="" />
          <span>周榜单</span>
        </li>
        <li @click="catehot">
          <img src="http://xinpianchang.oss.aliyuncs.com/home_hot_icon/cate_hot_3x.png" alt="" />
          <span>分类热门</span>
        </li>
        <li @click="cateedit">
          <img src="https://xinpianchang.oss.aliyuncs.com/home_hot_icon/cate_selection_3x.png" alt="" />
          <span>分类精选</span>
        </li>
        <li @click="catepicker">
          <img src="https://xinpianchang.oss.aliyuncs.com/home_hot_icon/vmovier_3x.png" alt="" />
          <span>场库精选</span>
        </li>
      </ul>
    </div>
    <!-- 热门列表 -->
    <div class="video_list" v-if="hotlist">
      <ul>
        <li v-for="(item, index) in hotlist" :key="item.children[0].model.resource.id"
          @click.stop="click_Hot_video(item.children[0].model.resource.id, item.children[0].model.resource.video_library_id)">
          <div class="content">
            <div class="user">
              <div class="uer_left">
                <div class="user_image" @click.stop="click_user(item.children[0].model.resource.author.userinfo.id)">
                  <!-- <img src="../assets/images/swiper1.webp" /> -->
                  <img :src="item.children[0].model.resource.author.userinfo.avatar+'?param=5y5'" />
                  <div class="smallimage" v-if="item.children[0].model.resource.author.userinfo.vip_flag == 1"
                    style="background-color: #fff;">
                    <img src="../assets/images/177会员、vip.png" alt="" />
                  </div>
                </div>
                <div class="user_name">
                  {{ item.children[0].model.resource.author.userinfo.username }}
                  <span v-if="item.children[0].model.resource.author.userinfo.user_status.follow_status">和其他<em>9</em>
                    人</span>
                </div>
              </div>
              <div class="user_right">
                <div class="addfollow" @click.stop="attentiontaggtrue(item.children[0].model.resource.author.userinfo.id)"
                  v-if="!idarr.includes(item.children[0].model.resource.author.userinfo.id)||!istoken">+ 关注</div>
                <div class="addfollow"
                  @click.stop="attentiontaggfalse(item.children[0].model.resource.author.userinfo.id)" v-else>已关注
                </div>
                <div class="logo"><span class="iconfont icon-gengduo"></span></div>
              </div>
            </div>
            <div class="word">{{ item.children[0].model.resource.title }}</div>
            <!-- <div class="video" v-show="index == scrollIndex"  style=" background: #000;">
              <video :src="videolisturl[index]" controls muted autoplay></video>
            </div> -->
            <div class="video">
              <img :src="item.children[0].model.resource.cover+'?param=5y5'" />
              <div class="mask">
                {{ viewCount(item.children[0].model.resource.count.count_view) }}观看 |
                {{ vidotime(item.children[0].model.resource.duration) }}
              </div>
            </div>
            <div class="bottom">
              <ul>
                <li @click.stop="collectFuns(item.children[0].model.resource.id)"
                  v-if="!collectlikearr.includes(item.children[0].model.resource.id)||!istoken"><span
                    class="iconfont icon-31shoucang"></span>{{
                      viewCount(item.children[0].model.resource.count.count_collect) }}
                </li>
                <li @click.stop="uncollectFuns(item.children[0].model.resource.id)" v-else><span
                    class="iconfont icon-wodeshoucang" style="color: yellow"></span>{{
                      viewCount(item.children[0].model.resource.count.count_collect + 1) }}</li>

                <li>
                  <span class="iconfont icon-review"></span>{{
                    viewCount(item.children[0].model.resource.count.count_comment) }}
                </li>
                <li @click.stop="likeFun(item.children[0].model.resource.id)"
                  v-if="!likearr.includes(item.children[0].model.resource.id)||!istoken"><span
                    class="iconfont icon-dianzan"></span>{{
                      viewCount(item.children[0].model.resource.count.count_like) }}
                </li>
                <li @click.stop="unlikeFun(item.children[0].model.resource.id)" v-else><span
                    class="iconfont icon-dianzan_kuai" style="color: red;"></span>{{
                      viewCount(item.children[0].model.resource.count.count_like + 1) }}</li>
              </ul>
            </div>
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>
<style lang="scss">
.hotcom {
  padding: 0 11px;
  padding-top: 6px;
  background-color: #f7f7f7;

  .banners {
    width: 100%;
    height: 199px;
    border-radius: 10px;
    overflow: hidden;

    img {
      width: 100%;
      height: 100%;
      display: block;
    }
  }

  .btnlist {
    height: 100px;
    display: flex;
    align-items: center;
    width: 100%;

    ul {
      display: flex;
      width: 100%;

      align-items: center;

      li {
        width: 25%;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-direction: column;
        color: #060606;
        font-size: 12px;

        img {
          width: 39px;
          height: 39px;
          margin-bottom: 6px;
        }
      }
    }
  }

  .video_list {
    &>ul {
      &>li {
        background-color: #fff;
        width: 100%;
        // height: 320px;
        border-radius: 10px;
        margin-bottom: 12px;
        border-top: 0.1px solid transparent;

        .content {
          .user {
            padding: 0 12px;
            margin-top: 12px;
            display: flex;
            justify-content: space-between;

            .uer_left {
              display: flex;

              .user_image {
                margin-right: 5px;
                position: relative;

                img {
                  border-radius: 50%;
                  width: 32px;
                  height: 32px;
                }
                .smallimage {
                position: absolute;
                top: 20px;
                right: -3px;
                width: 13px;
                height: 13px;
                display: flex;
                align-items: center;
                justify-content: center;
                background-color: rgb(255, 255, 255);
                border-radius: 50%;

                img {
                  width: 13px;
                  height: 13px;
                  border-radius: 50%;
                }
              }
              }

              .user_name {
                font-size: 12px;
                color: #484848;
                line-height: 32px;

                span {
                  color: #767676;

                  em {
                    color: #1c1c1c;
                    font-weight: 600;
                    font-style: normal;
                    margin-left: 4px;
                  }
                }
              }
            }

            .user_right {
              display: flex;

              .addfollow {
                color: #eb4b3a;
                font-size: 12px;
                margin-right: 19px;
                font-weight: 600;
              }

              .logo {
                span {
                  color: #8a8a8a;
                }
              }
            }
          }

          .word {
            padding: 0 12px;
            font-size: 14px;
            color: #282828;
            font-weight: 600;
            line-height: 31px;
          }

          .video {
            position: relative;
            height: 203px;
            display: flex;
          align-items: center;
            video{
              width:100%;
              height: 100%;
              display: block;
            }
            img {
              width: 100%;
              height: 199px;
              display: flex;
            }

            .mask {
              position: absolute;
              bottom: 0;
              right: 0;
              color: #fff;
              font-size: 12px;
              margin-right: 12px;
              line-height: 30px;
            }
          }

          .bottom {
            &>ul {
              display: flex;
              align-items: center;

              &>li {
                width: 33.33%;
                text-align: center;
                line-height: 44px;
                font-size: 12px;
                display: flex;
                align-items: center;
                justify-content: center;

                span {
                  margin-right: 7px;
                  font-size: 20px;
                  color: #6a6869;
                }
              }
            }
          }
        }
      }
    }
  }
}</style>